package com.lagou.server.web.controller;

import com.lagou.server.api.ShoppingService;
import com.lagou.server.api.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class UserController {


    private ExecutorService executorService = new ThreadPoolExecutor(100,200,5, TimeUnit.SECONDS,new LinkedBlockingDeque<>(2000));

    @DubboReference(loadbalance ="roundrobin" )
    private UserService userService;

    @GetMapping(value = "/show")
    public String show(String name){
        return userService.show(name);
    }


    @DubboReference
    private ShoppingService shoppingService;

    @GetMapping(value = "/batch/order")
    public String order(int orderSize){

        for (int i = 0;i<=orderSize;i++){

            executorService.execute(()->{
                log.info( shoppingService.order());
                log.info( shoppingService.pay());
                log.info( shoppingService.deliver());
            });
        }
        return "下单完成";
    }

}
